package com.dyava.commons.clock.mysql;

import com.dyava.commons.clock.ClockMessage;
import com.dyava.commons.clock.ClockSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Date;

/**
 * Created by jjliu on 2020/4/17.
 */
public class MySqlClockSender extends ClockSender implements InitializingBean{
    static Logger log = LoggerFactory.getLogger(MySqlClockSender.class);


    JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("-> 尝试创建dy_clock_record");
        jdbcTemplate.execute(MysqlClockConstant.SQL_CREATE);
    }

    @Override
    protected void send(ClockMessage clockMessage, long timestamp) {
        log.trace("-> 插入clockMessage");
        try {
            jdbcTemplate.update(MysqlClockConstant.SQL_INSERT,clockMessage.getHandleName(),clockMessage.getArgs(),0,new Date(timestamp), new Date());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
